package com.lyp.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.lyp.adapters.CarTaskDetailsAdapter;
import com.lyp.jsonbean.AllTaskBean;
import com.lyp.jsonbean.CarOrdersDetailsBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
//车内任务详情
public class CarTaskDetailsActivity extends Activity {

    private AllTaskBean.DataBean mCarBean;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private CarTaskDetailsAdapter mAdapter;//车内任务详情
    private List<CarOrdersDetailsBean.DataBean> mList = new ArrayList<>();
    private Button mRece;
    private ImageView mBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_taskdetails);
        init();
    }

    private void init() {
        mCarBean = (AllTaskBean.DataBean) getIntent().getExtras().get("databean");
        mRecyclerView = (RecyclerView) findViewById(R.id.taskdetails_recycler);
        mBack = (ImageView) findViewById(R.id.iv_cartask_back);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mAdapter = new CarTaskDetailsAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        //车内接单按钮
        mRece = (Button) findViewById(R.id.btn_order_receiving);
        mRece.setVisibility(View.GONE);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        try {
            carTaskDetails(mCarBean.getO_id());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //车内任务详情
    public void carTaskDetails(String o_id) throws IOException{
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<CarOrdersDetailsBean> call = api.getCarOrders(o_id);
        call.enqueue(new Callback<CarOrdersDetailsBean>() {
            @Override
            public void onResponse(Call<CarOrdersDetailsBean> call, final Response<CarOrdersDetailsBean> response) {
                if (response.body().getResult() == 1 ) {
                    CarTaskDetailsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mList.addAll(response.body().getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                }
            }
            @Override
            public void onFailure(Call<CarOrdersDetailsBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }
}